package com.example.huadu_server.oss.config;

import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.Configuration;
import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Data
public class OssConfig {

    @Value("${oss.accessKey}")
    private String accessKey;

    @Value("${oss.secretKey}")
    private String secretKey;

    @Value("${oss.bucket}")
    private String bucket;

    @Value("${oss.domain}")
    private String domain;

    // 初始化上传管理器
    public UploadManager getUploadManager() {
        // 使用华南区域 (z2)
        Configuration config = new Configuration(Region.region2());
        return new UploadManager(config);
    }

    // 获取认证信息
    public Auth getAuth() {
        return Auth.create(accessKey, secretKey);
    }

    // 获取上传凭证
    public String getUploadToken() {
        return getAuth().uploadToken(bucket);
    }
}

